import { customRef } from "vue";

export const getMsg = (initValue: string, delay: number) => {
  let timer;
  const msg = customRef((track, trigger) => {
    return {
      get() {
        track(); //对这个数据进行持续关注，一旦变化就更新
        return initValue;
      },
      set(value) {
        clearTimeout(timer);
        timer = setTimeout(() => {
          initValue = value;
          trigger(); //告诉vue数据发生了变化
        }, delay);
      },
    };
  });

  return { msg };
};
